/**
 * 头部模块
 */
define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";



	
	$(function(){
		//$(window).bind('scroll',function(){scrollLoad()}); 
		$(document).on("click","#ldh-header-left .ldh-header-nav >p",function(){
			if(!$(this).parent().hasClass("on")){
				$(this).parent().addClass("on").siblings(".ldh-header-nav").removeClass("on");	
			}else{$(this).parent().removeClass("on");}
			$(document).on("click",function(event){
				var _target=$(event.target).parents();
				if(!_target.hasClass("ldh-header-nav")){$("#ldh-header-left .ldh-header-nav").removeClass("on");}			
			});
		});
		//商家主页一级分类切换
		$(document).on("click",".ldh-header-classNav span",function(){
			$(this).addClass("on").siblings().removeClass("on")	
		});
		$(document).on("focus","a,input:button,input:submit,button,input:checkbox,input:radio",function(){this.blur()});
		$(document).on("click",".ldh-user >p",function(){
			if($(this).find("span:eq(1)").text()=="登录")
				return;
			
			if(!$(this).parent().hasClass("on")){
				$(this).parent().addClass("on");
			}else{$(this).parent().removeClass("on");}
			$(document).on("click",function(event){
				var _target=$(event.target).parents();
				if(!_target.hasClass("ldh-user")){$(".ldh-user").removeClass("on");}								
			});
			$(".ldh-user li").click(function(){$(".ldh-user").removeClass("on");})
		});
	});
	
	//分类没有找到商家
	function unfoundStore(){
		$(".ldh-header-storeList .ldh-class-list dl").append('<dd class="ldh-unfoundStore">附近没有该分类的商家</dd>');	
	}
		
	var base = require("../../custom/base/component-base.js");
	
	base.init(base);
	
	require("../../custom/base/component-core.js");
	
	var async = require("../../custom/base/component-async.js");

	var validate = require("../../custom/base/component-validate.js");
	
	//初始化函数
	exports.init=function(m,isLogin){
		
		window.$ldh_head=m;
		
		/*exports.showAllShopsTypes();*/
		
		if(isLogin){
			
			var userId=$("#session_userid").val();
			
			if(!validate.isNull(userId)){
				
				base.hasLoginShow(userId,$("#session_username").val(),$("#session_iconurl").val());
			}
		}
		
		
	};
	
	//查询商家分类
	exports.showAllShopsTypes=function(obj){
		
		if($(obj).hasClass("on"))
			return;
	
		$("#parentTypeList").html("");
		
		base.showLoading("#parentTypeList",20);
		
		eval(
				Wind.compile("async", function() {

					var data = $await(async.DDF_AsyncRequest("POST", ctx + "/shopweb/ShopsType/showAllShopsTypes.do", ""));
					var parentHtmlStr="";
					if(data["code"]==1){
						for (var i = 0; i < data["shopTypeList"].length; i++) {
							
							
							if(data["shopTypeList"][i]["shopTypeId"]=="1"){
								
								parentHtmlStr+='<span class="color-food on" onclick=$ldh_head.getShopListByType(this,"'+data["shopTypeList"][i]["shopTypeId"]+'")>'+data["shopTypeList"][i]["shopTypeName"]+'</span>';

							}else if(data["shopTypeList"][i]["shopTypeId"]=="2"){
								parentHtmlStr+='<span class="color-goods" onclick=$ldh_head.getShopListByType(this,"'+data["shopTypeList"][i]["shopTypeId"]+'")>'+data["shopTypeList"][i]["shopTypeName"]+'</span>';

							}else if(data["shopTypeList"][i]["shopTypeId"]=="3"){
								
								parentHtmlStr+='<span class="color-services" onclick=$ldh_head.getShopListByType(this,"'+data["shopTypeList"][i]["shopTypeId"]+'")>'+data["shopTypeList"][i]["shopTypeName"]+'</span>';

							}else if(data["shopTypeList"][i]["shopTypeId"]=="4"){
								
								parentHtmlStr+='<span class="color-feature" onclick=$ldh_head.getShopListByType(this,"'+data["shopTypeList"][i]["shopTypeId"]+'")>'+data["shopTypeList"][i]["shopTypeName"]+'</span>';

							}else if(data["shopTypeList"][i]["shopTypeId"]=="5"){
								
								parentHtmlStr+='<span class="color-express" onclick=$ldh_head.getShopListByType(this,"'+data["shopTypeList"][i]["shopTypeId"]+'")>'+data["shopTypeList"][i]["shopTypeName"]+'</span>';

							}else if(data["shopTypeList"][i]["shopTypeId"]=="50"){
								
								parentHtmlStr+='<span class="color-fresh" onclick=$ldh_head.getShopListByType(this,"'+data["shopTypeList"][i]["shopTypeId"]+'")>'+data["shopTypeList"][i]["shopTypeName"]+'</span>';

							}
							
							if(i==0){
								exports.getShopListByType(data["shopTypeList"][i]["shopTypeId"]);
							}
						}
					}
					$("#parentTypeList").html(parentHtmlStr);
					
				}))().start();
		
	};
	
	//根据分类查询商家
	exports.getShopListByType=function(obj,shopType){

		
		
		var lng=base.getCookie("lng");
		
		var lat=base.getCookie("lat");
		
		var params={
			shopType:shopType,		
			lng:lng,
			lat:lat
		};
		
		$("#div_shoplist").show();
		
		base.showLoading("#div_shoplist",50);
		
		$("#shopTypeShopList").html("");
		
		eval(
				Wind.compile("async", function() {

					var data = $await(async.DDF_AsyncRequest("POST", ctx + "/shopweb/Shop/getShopListByType.do", params));
					
					if(data["code"]==1){
						
						for (var i = 0; i < data["shopList"].length; i++) {
								
								for(var j=0;j<data["shopList"][i].length;j++){
									
									
										var parentId=data["shopList"][i][j]["parentId"];
										
										var logoUrl=data["shopList"][i][j]["logoUrl"];
										
										if(logoUrl=="" || logoUrl.indexOf(".")<0){
											
											logoUrl= ctx+'/v1/img/category_f.png';
										}else{
											
											logoUrl=fileOpenUrl+logoUrl;
										} 
									//	$("#shopTypeShopList").append('<li onclick=$ldh_base.jumpToShopIndex("'+data["shopList"][i][j]["shopId"]+'")><span class="ldh-storeExp-pic"><img src="'+logoUrl+'"></span><span>'+data["shopList"][i][j]["shopName"]+'</span></li>');
							
										$("#shopTypeShopList").append('<dd onclick=$ldh_base.jumpToShopIndex("'+data["shopList"][i][j]["shopId"]+'") title="'+data["shopList"][i][j]["shopName"]+'"><span class="ldh-class-pic2"><img src="'+logoUrl+'" /></span><span class="ldh-class-name">'+base.subString(data["shopList"][i][j]["shopName"],9)+'</span></dd>');								}
						}


						base.cancelLoading("#div_shoplist");
						
						
						if(data["shopList"].length==0){
							
							unfoundStore();
					
						}
						
					}
					
					
				}))().start();
		
	};
	



	
});

